package com.zhuang.designpattern.singleton;

/**
 * description: 双重锁校验(线程安全)
 * date: 2022/9/14 18:31
 * author: Zhuang
 * version: 1.0
 */
public class Singleton_05 {
    public static void main(String[] args) {
        Singleton05 instance1 = Singleton05.getInstance();
        Singleton05 instance2 = Singleton05.getInstance();

        System.out.println("instance1 = " + instance1);
        System.out.println("instance2 = " + instance2);
    }
}

class Singleton05 {
    private static Singleton05 instance;

    private Singleton05() {

    }

    public static Singleton05 getInstance() {
        if (null != instance) return instance;
        synchronized (Singleton05.class) {
            if (null == instance) {
                instance = new Singleton05();
            }
        }
        return instance;
    }
}